Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid division in round_up #1343

Closed
wants to merge 2 commits into from
Closed

Conversation

VoxSciurorum
Copy link
Contributor

Round up to page size using add and mask instead of divide and multiply. This is slightly faster, but more importantly it avoids a function call when division is not implemented in hardware. Looking up the target of the call here can lead to deadlock in rtld. See recent discussion on -arm and -current.

Some older armv7 CPUs do not have hardware divide. Theoretically FreeBSD could also target a RISC-V CPU without hardware multiply and divide.

size = ((size / _thr_page_size) + 1) *
_thr_page_size;
return size;
return (size + _thr_page_size - 1) & ~(size_t)(_thr_page_size - 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to use roundup2(size, _thr_page_size)? It's either a compiler buildit or the same code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot about roundup2. Konstantin Belousov sent a diff to the mailing lists using that function.

Division may require a function call, leading to
dynamic function lookup, leading to deadlock in rtld.
freebsd-git pushed a commit that referenced this pull request Jul 25, 2024
Division may require a function call, leading to
dynamic function lookup, leading to deadlock in rtld.

Pull Request:	#1343
freebsd-git pushed a commit that referenced this pull request Jul 25, 2024
This reverts commit 2b22973.

Pull Request:	#1343
size = ((size / _thr_page_size) + 1) *
_thr_page_size;
return size;
return (roundup2(size, _thr_page_size - 1));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return (roundup2(size, _thr_page_size - 1));
return (roundup2(size, _thr_page_size));

I'd merged this, but Doug Moore correctly pointed out that this is wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Too many versions of code going around and I stopped thinking about each new one. It will usually work, but the - 1 is wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed a fix to the branch associated with this pull request.

not the mask equal to page size - 1.
brooksdavis pushed a commit to brooksdavis/freebsd that referenced this pull request Jul 26, 2024
Division may require a function call, leading to
dynamic function lookup, leading to deadlock in rtld.

Pull Request:	freebsd#1343
@bsdimp bsdimp closed this Jul 29, 2024
@bsdimp bsdimp added the merged label Jul 29, 2024
@lwhsu
Copy link
Member

lwhsu commented Jul 29, 2024

@brooksdavis
Copy link
Contributor

FYI this was reverted in f5c8944 as: https://lists.freebsd.org/archives/dev-commits-src-all/2024-July/043646.html

kib beat to to remerging it with 31f688a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants